/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.report.userStat.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.bussiness.report.userStat.domain.UserStat;
import com.seeyoui.kensite.bussiness.report.userStat.domain.UserStatVO;
import com.seeyoui.kensite.bussiness.report.userStat.service.UserStatService;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.domain.Page;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.util.StringUtils;
import com.seeyoui.kensite.common.util.excel.ExportExcel;
import com.seeyoui.kensite.framework.cms.article.domain.ArticleVO;
import com.seeyoui.kensite.framework.cms.article.service.ArticleQueryService;
import com.seeyoui.kensite.framework.system.util.UserUtils;
/**
 * UserStat
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-01-12
 */

@Controller
@RequestMapping(value = "report/userStat")
public class UserStatController extends BaseController {
	
	@Autowired
	private UserStatService userStatService;
	@Autowired
	private ArticleQueryService articleQueryService;
	
	/**
	 * 展示列表页面
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:view")
	@RequestMapping(value = "/{page}")
	public ModelAndView view(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page) throws Exception {
		return new ModelAndView("bussiness/report/userStat/"+page, modelMap);
	}
	
	/**
	 * 根据ID查询单条数据
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:select")
	@RequestMapping(value = "/data/{id}")
	@ResponseBody
	public Object data(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String id) throws Exception {
		UserStat userStat = userStatService.findOne(id);
		return userStat;
	}
	
	/**
	 * 根据ID查询单条数据并返回相应表单
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:view")
	@RequestMapping(value = "/form/{page}/{id}")
	public ModelAndView form(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page, @PathVariable String id) throws Exception {
		UserStat userStat = userStatService.findOne(id);
		modelMap.put("userStat", userStat);
		return new ModelAndView("bussiness/report/userStat/"+page, modelMap);
	}
	
	/**
	 * 获取列表展示数据
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:select")
	@RequestMapping(value = "/list/data", method=RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		List<UserStat> userStatList = userStatService.findList(userStat);
		int total = userStatService.findTotal(userStat);
		EasyUIDataGrid eudg = new EasyUIDataGrid();
		eudg.setTotal(String.valueOf(total));
		eudg.setRows(userStatList);
		return eudg;
	}
	
	/**
	 * 获取列表展示数据带分页器用于jsp自己做分页
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:view")
	@RequestMapping(value = "/list/page", method=RequestMethod.POST)
	@ResponseBody
	public Object listPage(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		List<UserStat> userStatList = userStatService.findList(userStat);
		int total = userStatService.findTotal(userStat);
		Page<UserStat> page = new Page<UserStat>(userStat.getPage(), userStat.getRows(), total);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("rows", userStatList);
		dataMap.put("page", page);
		return dataMap;
	}
	
	/**
	 * 获取所有数据
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:select")
	@RequestMapping(value = "/list/all", method=RequestMethod.POST)
	@ResponseBody
	public Object listAll(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		List<UserStat> userStatList = userStatService.findAll(userStat);
		return userStatList;
	}
	
	/**
	 * 保存新增的数据
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:insert")
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		if (!beanValidator(modelMap, userStat)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		userStatService.save(userStat);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 保存修改的数据
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:update")
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		if (!beanValidator(modelMap, userStat)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		userStatService.update(userStat);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 删除数据库
	 * @param modelMap
	 * @param userStatId
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:delete")
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String id) throws Exception {
		List<String> listId = Arrays.asList(id.split(","));
		userStatService.delete(listId);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 假删除
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:delete")
	@RequestMapping(value = "/remove", method=RequestMethod.POST)
	@ResponseBody
	public String remove(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		userStatService.remove(userStat);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 导出Excel数据
	 * @param modelMap
	 * @param userStat
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("report:userStat:export")
	@RequestMapping(value = "/export")
	public String export(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, UserStat userStat) throws Exception {
		String fileName = DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
		List<UserStat> userStatList = userStatService.findAll(userStat);
		new ExportExcel(null, UserStat.class).setDataList(userStatList).write(response, fileName).dispose();
		return null;
	}
	
	/**
	 * 查询指定用户ID的统计记录
	 * @param type 不为空则是为手机端提供数据，需要添加最近获得积分的文章列表
	 * @return Result
	 * @throws Exception
	 */
	@RequestMapping(value = "/findByUserId", method=RequestMethod.POST)
	@ResponseBody
	public Result findByUserId(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String type) throws Exception {
		String userId = UserUtils.getUser(request).getId();
		UserStat userStat = userStatService.findByUserId(userId);
		if(StringUtils.isEmpty(type)) {
			return Result.success(UserStatVO.instance(userStat));
		}
		
		List<ArticleVO> lsArticleVOs = articleQueryService.findArticleForPoint(userId);
		return Result.success(UserStatVO.instance(userStat, lsArticleVOs));
	}
	
	/**
	 * 获取积分排名前20名
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findTop20", method=RequestMethod.POST)
	@ResponseBody
	public Result findTop20(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) throws Exception {
		return Result.success(userStatService.findTop20());
	}
	
	/**
	 * 获取全部积分排名
	 * @param session
	 * @param response
	 * @param request
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findAllRank", method=RequestMethod.POST)
	@ResponseBody
	public Object findAllRank(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) throws Exception {
		return Result.success(userStatService.findAllRank());
	}
	
	@RequestMapping("/goJifenDetail")
	public String goJifenDetail(){
		return "partybuild/jifen_detail";
	}
	
}